<?php

namespace Tasker\Models;

class UsersModel extends BaseModel
{

	/**
	 *
	 * @var \Tasker\Authentication\Authenticator
	 */
	protected $authenticator;

	public function __construct(\NotORM $connection, \Tasker\Authentication\Authenticator $authenticator)
	{
		parent::__construct($connection);
		$this->authenticator = $authenticator;
	}

	public function getUsers(array $where=array())
	{
		return $this->connection->user()->where($where)->order('surname,name');
	}

	public function getUser(array $where)
	{
		return $this->connection->user[$where];
	}

	public function addUser($values)
	{
		$skills = $values['skills'];
		if($skills == null)
			$skills = array();
		unset($values['skills']);
		if (isset($values['password']))
		{
			$dynamicSalt = \Nette\Utils\Strings::random(23);
			$values['password'] = $this->authenticator->calculateHash($values['password'], $dynamicSalt) . ":" . $dynamicSalt;
		}
		$inserted = $this->connection->user()->insert((array) $values);
		if ($inserted != null)
		{
			foreach ($skills as $skill)
			{
				$l_values = array('user_id' => $inserted['id'], 'skill_id' => $skill);
				$this->connection->user_skill()->insert($l_values);
			}
		}
		return $inserted;
	}

	public function editUser($values)
	{
		$id = $values['id'];
		$skills = $values['skills'];
		if($skills == null)
			$skills = array();
		unset($values['id'], $values['skills']);
		if (isset($values['password']))
		{
			$dynamicSalt = \Nette\Utils\Strings::random(23);
			$values['password'] = $this->authenticator->calculateHash($values['password'], $dynamicSalt) . ":" . $dynamicSalt;
		}
		$affected = $this->connection->user[$id]->update((array) $values);
		if ($affected !== false)
		{
			$this->connection->user_skill()->where('user_id', $id)->delete();
			foreach ($skills as $skill)
			{
				$l_values = array('user_id' => $id, 'skill_id' => $skill);
				$this->connection->user_skill()->insert($l_values);
			}
		}
		return $affected;
	}
	
	public function editProfile($values)
	{
		$id = $values['id'];
		if (isset($values['password']))
		{
			$login = $this->connection->user[$id]['login'];
			$this->authenticator->checkUser(array($login,$values['password_old']));
			$dynamicSalt = \Nette\Utils\Strings::random(23);
			$values['password'] = $this->authenticator->calculateHash($values['password'], $dynamicSalt) . ":" . $dynamicSalt;
		}
		unset($values['password_old']);
		$this->connection->user[$id]->update((array) $values);
		
		
	}

	public function deleteUser($id)
	{
		return $this->connection->user[$id]->delete();
	}

	public function getUsersSkills(array $where=array())
	{
		return $this->connection->user_skill()->where($where);
	}

	public function getSkills(array $where=array())
	{
		return $this->connection->skill()->where($where);
	}

	public function getDepartments(array $where=array())
	{
		return $this->connection->department()->where($where);
	}

}
